package com.sinoglobal.ningxia.activity.vip;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.activity.base.BaseActivity;
import com.sinoglobal.ningxia.beans.RootVo;
import com.sinoglobal.ningxia.downdata.imp.RemoteImpl;
import com.sinoglobal.ningxia.frame.FlyApplication;
import com.sinoglobal.ningxia.http.Code;
import com.sinoglobal.ningxia.utils.MContants;
import com.sinoglobal.ningxia.utils.SharedPreferenceUtil;
import com.sinoglobal.ningxia.utils.TextUtil;
import com.sinoglobal.ningxia.utils.ValidUtil;
/**
 * 
* @title: ChangePwdActivity.java 
* @package com.sinoglobal.ningxia.activity.vip 
* @description: 修改密码 
* @author qwx
* @date 2014-10-16 上午11:51:19
 */
public class PasswordChange extends BaseActivity implements OnClickListener, OnFocusChangeListener {
	// 旧密码
	private EditText oldPwd;
	// 新密码
	private EditText newPwd;
	// 确认密码
	private EditText confirmPwd;
	// 提交
	private Button submit;
	// 错误信息提示
	private TextView erroInfo;
	// 错误信息提示标志位
	private boolean flag = false;
	// 加载数据
	private ItktAsyncTaskWithDialog<Void, Void, RootVo> mTask;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_vip_change_pwd);
		openChenJInShi();//开启沉浸式
		init();
		setListener();
	}

	private void init() {
		titleView.setText(getString(R.string.change_password));
		oldPwd = (EditText) findViewById(R.id.old_password);
		newPwd = (EditText) findViewById(R.id.new_password);
		confirmPwd = (EditText) findViewById(R.id.confirm_password);
		submit = (Button) findViewById(R.id.submit);
		erroInfo = (TextView) findViewById(R.id.errorInfo);
	}

	private void setListener() {
		submit.setOnClickListener(this);
		oldPwd.setOnFocusChangeListener(this);
		newPwd.setOnFocusChangeListener(this);
		confirmPwd.setOnFocusChangeListener(this);
	}

	// 判断新密码和确认密码是否匹配
	private boolean isNewPwdMatchConfirmPwd(String newPwd, String confirmPwd) {
		boolean result = false;
		if (newPwd.equals(confirmPwd))
			result = true;
		else
			result = false;
		return result;

	}

	// 校验输入的密码，包括旧密码、新密码、确认密码
	private void validInputPwd() {
		String info = "密码仅支持6-18位字母、数字";
		String oldPwdStr;
		String newPwdStr;
		String confirmPwdStr;
		oldPwdStr = oldPwd.getText().toString().trim();
		newPwdStr = newPwd.getText().toString().trim();
		confirmPwdStr = confirmPwd.getText().toString().trim();
		//旧密码
		if (TextUtil.stringIsNull(oldPwdStr)) {
			erroInfo.setVisibility(View.VISIBLE);
			if (oldPwdStr.length() > 0)
				erroInfo.setText(info);
			else
				erroInfo.setText(getString(R.string.please_input_oldpwd));
			return;
		} else {
			if (!validPwd(oldPwd))
				return;

		}
		
		//新密码
		if (TextUtil.stringIsNull(newPwdStr)) {
			erroInfo.setVisibility(View.VISIBLE);
			if (newPwdStr.length() > 0)
				erroInfo.setText(info);
			else
				erroInfo.setText(getString(R.string.please_input_newpwd));
			return;
		} else {
			if (!validPwd(newPwd))
				return;
		}
		//确认密码
		if (TextUtil.stringIsNull(confirmPwdStr)) {
			erroInfo.setVisibility(View.VISIBLE);
			if (confirmPwdStr.length() > 0)
				erroInfo.setText(info);
			else
				erroInfo.setText(getString(R.string.please_input_confirmpwd));
			return;
		} else {
			if (!validPwd(confirmPwd))
				return;
		}
		
		//新密码和确认密码是否匹配
		if (erroInfo.getVisibility() == View.VISIBLE) {
			return;
		} else if (isNewPwdMatchConfirmPwd(oldPwdStr, confirmPwdStr)) {
			erroInfo.setText(getString(R.string.new_isNot_old));
			erroInfo.setVisibility(View.VISIBLE);
			return;
		} else if (!isNewPwdMatchConfirmPwd(newPwdStr, confirmPwdStr)) {
			erroInfo.setText(R.string.please_input_samepwd);
			erroInfo.setVisibility(View.VISIBLE);
			return;
		} else {
			if (flag) {
				erroInfo.setVisibility(View.GONE);
				ChangePwd();
			}

		}

	}

	// 修改密码
	private void ChangePwd() {
		mTask = new ItktAsyncTaskWithDialog<Void, Void, RootVo>(getResources().getString(R.string.searching_infor), true, true) {
			@Override
			public RootVo before(Void... params) throws Exception {
				String username = SharedPreferenceUtil.getString(getApplicationContext(), "username");
				return RemoteImpl.getInstance().changePwd(username, oldPwd.getText().toString().trim(), newPwd.getText().toString().trim());
			}

			@Override
			public void after(RootVo result) {
				if (Code.CODE_SUCCESS == result.getCode()) {
					showShortToastMessage(PasswordChange.this.getString(R.string.password_success));
					FlyApplication.userId = MContants.EMPTY_STRING;// 退出登录是userId为空
					setShareData(MContants.EMPTY_STRING);
					Intent go = new Intent(FlyApplication.context, Login.class);
					startActivity(go);
					finish();

				} else {
					showShortToastMessage(result.getMessage());
				}
			}

			@Override
			public void exception() {
			}
		};
		mTask.execute();
	}

	// 将登录信息保存为空值并移除记录的密码
	private void setShareData(String value) {
		SharedPreferenceUtil.saveString(FlyApplication.context, "id",value);
		SharedPreferenceUtil.saveString(FlyApplication.context, "photo",value);
		SharedPreferenceUtil.saveString(FlyApplication.context, "sex",value);
		SharedPreferenceUtil.saveString(FlyApplication.context, "nickname",value);
		SharedPreferenceUtil.saveString(FlyApplication.context, "score",value);
		SharedPreferenceUtil.saveString(FlyApplication.context, "autoPwd",value);
		SharedPreferenceUtil.saveBoolean(FlyApplication.context, "autoLogin", false);
 	}
	
	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.submit:
			validInputPwd();
			break;
		}

	}

	// 验证输入的密码
	private boolean validPwd(EditText pwdEdit) {
		boolean result = false;
		String pwdStr = pwdEdit.getText().toString().trim();// 密码字符串
		String validPassword = ValidUtil.validPassword(pwdStr);// 校验密码的结果字符串
		if (!TextUtil.stringIsNull(validPassword)) {
			erroInfo.setVisibility(View.VISIBLE);
			erroInfo.setText(validPassword);
			flag = false;
			result = false;

		} else {
			erroInfo.setVisibility(View.GONE);
			flag = true;
			result = true;
		}
		return result;
	}

	@Override
	public void onFocusChange(View view, boolean arg1) {
		/*
		 * String str_old; String str_new; String str_confirm; str_old =
		 * oldPwd.getText().toString(); str_new = newPwd.getText().toString();
		 * str_confirm = confirmPwd.getText().toString();
		 * 
		 * switch (view.getId()) { case R.id.old_password: if (view.hasFocus()
		 * == false) { if (TextUtil.stringIsNull(str_old)) {
		 * erroInfo.setVisibility(View.VISIBLE);
		 * erroInfo.setText(getString(R.string.please_input_oldpwd)); } else {
		 * validPwd(oldPwd); } } break; case R.id.new_password: if
		 * (view.hasFocus() == false) { if (TextUtil.stringIsNull(str_new)) {
		 * erroInfo.setVisibility(View.VISIBLE);
		 * erroInfo.setText(getString(R.string.please_input_newpwd)); } else {
		 * validPwd(newPwd); } } break; case R.id.confirm_password: if
		 * (view.hasFocus() == false) { if (TextUtil.stringIsNull(str_confirm))
		 * { erroInfo.setVisibility(View.VISIBLE);
		 * erroInfo.setText(getString(R.string.please_input_confirmpwd)); } else
		 * { validPwd(confirmPwd); } } break; }
		 */

	}
}
